package org.xxpay.manage.config.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.domain.BizResponse;
import org.xxpay.core.common.domain.XxPayPageRes;
import org.xxpay.core.entity.PayDivisionMch;
import org.xxpay.manage.common.ctrl.BaseController;
import org.xxpay.manage.common.service.CommonService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/config/division/mch")
public class PayDivisionMchController extends BaseController {

    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        PayDivisionMch condition = getObject(param, PayDivisionMch.class);
        int count = commonService.payDivisionMchService.count(condition);
        if(count == 0) return ResponseEntity.ok(XxPayPageRes.buildSuccess());
        List<PayDivisionMch> PayDivisionMchList = commonService.payDivisionMchService.select((getPageIndex(param) -1) * getPageSize(param), getPageSize(param), condition);
        return ResponseEntity.ok(XxPayPageRes.buildSuccess(PayDivisionMchList, count));
    }

    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request, @RequestParam("id") Long id) {
        PayDivisionMch record = commonService.payDivisionMchService.findById(id);
        return ResponseEntity.ok(XxPayPageRes.buildSuccess(record));
    }



    @RequestMapping("/update")
    @ResponseBody
    public ResponseEntity<?> update(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        PayDivisionMch PayDivisionMch = getObject(param, PayDivisionMch.class);
        int count = commonService.payDivisionMchService.update(PayDivisionMch);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseEntity<?> add(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        PayDivisionMch PayDivisionMch = getObject(param, PayDivisionMch.class);
        int count = commonService.payDivisionMchService.add(PayDivisionMch);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }
}
